/* ======================================  表单数据行操作  ====================================== */
const _ = {
	// 获取 form 数据的字段数据
	getRow(forms, name){
		if(!window.$fn.hasArray(forms)){
			window.$fn.toast('提交表单或搜索表单数据不存在，请检查')
			return []
		}
		const arr = forms.filter( v => v.name === name)
		if(window.$fn.hasArray(arr)){
			return arr[0]
		}else{
			window.$fn.toast( name + '不存在' );
			return []
		}
	},
	// 判断提交表单是否有 data
	setModalRows(forms, names, callback){
		let stack = []
		if(window.$fn.hasArray(names)){
			names.forEach(v=>{
				const { data } = _.getRow(forms, v)
				if(!window.$fn.hasArray(data)){
					stack.push(true)
				}
			})
		}else{
			const { data } = _.getRow(forms, names)
			if(!window.$fn.hasArray(data)){
				stack.push(true)
			}
		}
		
		
		const bool = window.$fn.hasArray(stack)
		if(bool){
			callback && callback()
		}
		
		return bool
	},
	// 设置单个表单
	setRow(forms,name, data){
		const rows = this.getRow(forms, name)
		rows.data = data
		rows.loading = false
		return data
	},
	// 设置多个表单
	setRows(forms, name, data){
		if(window.$fn.hasArray(forms)){
			forms.forEach(v => this.setRow(v,name, data))
		}else{
			this.setRow(forms,name, data)
		}
		return data
	},
	
	async setRowc(forms, name, fn){
		const arr = forms.filter( v => v.name === name)
		if(window.$fn.hasArray(arr)){
			const rows = arr[0]
			let data = rows.data
			if(!window.$fn.hasArray(data)){
				rows.data = await fn()
				rows.loading = false
			}else{
				data = []
			}
			return data
		}else{
			_.toast( name + '不存在' );
		}
	},
	// 禁用与启用行
	disabledRows(forms, names, bool){
		if(window.$fn.hasArray(names)){
			forms.forEach(v =>{
				names.forEach(m=>{
					if(v.name === m){
						v.disabled = bool
					}
				})
			})
		}else{
			forms.forEach(v =>{
				if(v.name === names){
					v.disabled = bool
				}
			})
		}
	},
	// 显示与隐藏
	hideRows(forms,names, bool){
		if(window.$fn.hasArray(names)){
			forms.forEach(v =>{
				names.forEach(m=>{
					if(v.name === m){
						v.hide = bool
					}
				})
			})
		}else{
			forms.forEach(v =>{
				if(v.name === names){
					v.hide = bool
				}
			})
		}
	},
	// 禁用与启用其它行
	disabledToRows({value, name, data, form}, names){
		if(window.$fn.hasArray(names)){
			data.forEach(v =>{
				names.forEach(m=>{
					if(v.name === m){
						v.disabled = !value
					}
				})
			})
		}else{
			data.forEach(v =>{
				if(v.name === names){
					v.disabled = !value
				}
			})
		}
		this.setState({ [form]: data })
	},
	// 显示与隐藏其它行
	hideToRows({value, name, data, form}, names){
		if(window.$fn.hasArray(names)){
			data.forEach(v =>{
				names.forEach(m=>{
					if(v.name === m){
						v.hide = !value
					}
				})
			})
		}else{
			data.forEach(v =>{
				if(v.name === names){
					v.hide = !value
				}
			})
		}
		this.setState({ [form]: data })
	},
	// 开启、禁用按钮
	onEnabledBtn(name, {value}){
		const { formData } = this.state
		const rows = formData.find(v=>v.name === name)
		rows.btn.disabled = !value
		clearTimeout(this.clear)
		this.clear = setTimeout(()=>{
			this.setState({ formData })
		},100)
	},
	// 显示、隐藏按钮
	onHideBtn(name, value){
		const { formData } = this.state
		const rows = formData.find(v=>v.name === name)
		rows.btn.hide = value
		clearTimeout(this.clear)
		this.clear = setTimeout(()=>{
			this.setState({ formData })
		},100)
	},
	// 选择下拉框，改变其它元素框的值
	setRowValue(name, {rows}, option){
		const opt = {
			...option
		}
		let { nameStr } = opt
		nameStr = nameStr ? nameStr : name
		
		let value = ''
		if(window.$fn.hasObject(rows)){
			value = rows[nameStr]
		}
		this.formRef.setValue({[name]: value })
	},
	// 科室联动查询
	onKeShi({value, forms}, { id= 'project_id' }){
		const { $http, $fn } = window
		if(value){
			$http.submit(null,'project-team/jobselect',{ param:{uuid:value}}).then((data)=>{
				$fn.getRow(forms, id).idStr = 'uuid'
				$fn.setRow( forms, id, data )
				this.setState({forms})
			})
		}else {
			$fn.getRow(forms, id).idStr = 'value'
			$fn.setRow( forms, id, this.resetData )
			this.setState({forms})
		}
	},
	// 设置单个 lable
	setLabel(forms, name, value){
		forms.forEach(v =>{
			if(v.name === name){
				v.label = value
			}
		})
	},
	// 设置单个 itemClassName
	setClassName(forms, name, value){
		forms.forEach(v =>{
			if(v.name === name){
				v.itemClassName = value
			}
		})
	},
};

export default _